package com.snail.readyvr.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.snail.readyvr.R;
import com.snail.readyvr.adapter.CommonAdapter;
import com.snail.readyvr.adapter.ViewHolder;
import com.snail.readyvr.ui.CommonTopBar;

import java.util.Arrays;
import java.util.List;

/**
 * Created by MRKING on 2016/11/21.
 */

public class LanguageDialog {


    public static class Builder {
        private SparseArray<ImageView> mAllImgs = new SparseArray<>();
        private Context mContext;
        private View mContentView;
        private CommonAdapter<String> mAdapter;
        private List<String> mTtems;
        private int mChekedItem;
        private DialogInterface.OnClickListener mOnClickListener;
        private ListView mListView;
        private String mTitle;

        public Builder(Context context) {
            mContext = context;
        }


        public Builder addSingleChoiceItems(String[] items, String title, int checkedItem, DialogInterface.OnClickListener listener) {
            mTtems = Arrays.asList(items);
            mChekedItem = checkedItem;
            mOnClickListener = listener;
            mTitle = title;
            return this;
        }

        public AlertDialog show() {

            mContentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_language_layout, null);
            AlertDialog.Builder builder =  new AlertDialog.Builder(mContext);
            builder.setCancelable(true);
            final AlertDialog dialog =  builder
                    .setView(mContentView)
                    .show();
            dialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent);
            dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
            mListView = (ListView) mContentView.findViewById(R.id.dialog_language_list);
            CommonTopBar commonTopBar = (CommonTopBar) mContentView.findViewById(R.id.dialog_top_bar);
            commonTopBar.midTextView.setText(mTitle);
            commonTopBar.setOnClickCommonTopBar(new CommonTopBar.OnClickCommonTopBar() {
                @Override
                public void onClickLeftView() {
                    dialog.dismiss();
                }

                @Override
                public void onClickRightView() {

                }
            });
            mAdapter = new CommonAdapter<String>(mContext, R.layout.dialog_language_item) {
                @Override
                public void convert(ViewHolder helper, String item, final int position) {

                    if (helper == null || item == null)
                        return;

                    ImageView selectedImg = helper.getView(R.id.language_selected_img);
                    if (mChekedItem == position)
                        selectedImg.setVisibility(View.VISIBLE);
                    else
                        selectedImg.setVisibility(View.GONE);
                    mAllImgs.put(position, selectedImg);
                    helper.setText(R.id.language_name, item)
                            .setOnClickListener(R.id.select_language_layout, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    changeState(position);
                                    if (mOnClickListener != null)
                                        mOnClickListener.onClick(dialog, position);
                                }

                            });
                }
            };
            mListView.setAdapter(mAdapter);
            mAdapter.addData(mTtems, true, false);
            return dialog;
        }

        private void changeState(int position) {
            for (int i = 0; i < mAllImgs.size(); i++) {
                mAllImgs.get(i).setVisibility(i == position ? View.VISIBLE : View.GONE);
            }
        }

    }
}
